package com.qianfeng.admin.controller;

import com.qianfeng.entity.dto.Goods;
import com.qianfeng.entity.vo.MyPage;
import com.qianfeng.http.Result;
import com.qianfeng.service.impl.GoodsService;
import com.qianfeng.service.impl.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping("list")
    public String list(Model model ,
                       @RequestParam(value = "goodsTypeId" , required = false , defaultValue = "0") Long goodsTypeId ,
                       @RequestParam(value = "pageNum" , required = false , defaultValue = "1") Long pageNum ){

        model.addAttribute("GoodsTypes" , goodsTypeService.list());
        MyPage<Goods> myPage = goodsService.getByGoodTypeId(goodsTypeId, pageNum);
        model.addAttribute("goodsList" , myPage.getObjList());
        model.addAttribute("pageList" , myPage.getPageList());
        model.addAttribute("goodsTypeId" , goodsTypeId);
        model.addAttribute("pageNum" , pageNum);
        return "goods/list";
    }

    @RequestMapping("up")
    @ResponseBody
    public Result<?> up(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.up(goodsId);
        return Result.success();

//        Goods goods = goodsService.getById(goodsId);
//        goods.setState(1L);
//        goodsService.saveOrUpdate(goods);
//        return Result.success();
    }

    @RequestMapping("down")
    @ResponseBody
    public Result<?> down(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.down(goodsId);
        return Result.success();
    }

    @RequestMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.removeById(goodsId);
        return Result.success();
    }




}
